//
//  HRVModel.swift
//  MYModule
//
//  Created by HFY on 2022/11/13.
//

import UIKit
import SQLite
import SwiftDate
import HandyJSON
import MYModule

open class HRVModel: NSObject, MYSQLiteTableiabe, FullDaySQLite, HandyJSON  {
    
    public var adress: String = ""
    
    public var id: Int64 = 0
    
    public var date: Date = Date().sqlDate()
    
    ///  hrv 一天的 hrvs
    public var hrvs: [Int] = [] {
        
        willSet {
            
            let arr = newValue.map({ i ->Int in
                if i > 1500 {
                    return 0
                }
                return i
            })
            self.hrvs = arr
        }
    }
    
    /// 上一次数据更新时间戳
    public var updateTime: Double = 0
    
    /// hrv 单位
    public let hrv_unit = "ms"
    
   
    
    public var avg: Int {
        
        var avg = 0
        let hrvs = hrvs.filter { $0 > 0 }
        if hrvs.isEmpty {
            return 0
        }
        avg = hrvs.reduce(0, { $0 + $1 })/hrvs.count
        return avg
        
    }
    
    public var max: Int {
        var maxValue = 0
        hrvs.forEach {
            maxValue = Swift.max(maxValue, $0)
        }
        
        return maxValue
    }
    
    public var min: Int {
        
        var minValue = 1500
        hrvs.forEach {
            
            if $0 > 0 {
                minValue = Swift.min(minValue, $0)
            }
        }
        
        if minValue == 1500 {
            minValue = 0
        }
        
        return minValue
    }
    
    
    public var isEmpty: Bool {
        
        return self.hrvs.filter { $0 > 0 }.isEmpty
        
    }
    
   
    required public override init() {}
    
    public func updateData() {
        
        let arr = hrvs.filter { $0 < 1500 }
        hrvs = arr
        
        guard let _ = HRVModel.day(date: date) else {
            
            insert()
            return
        }
        
        update()
        
    }
    
 
    /// 24小时睡眠HRV
    /// - Parameter date: 日期
    /// - Returns: []
    public static func getHRV24ForSleep(date: Date) ->[HRVModel] {
        
        let date = date.sqlDate()
        let yesterDate = date - 1.days
        
        var todyModel: HRVModel = HRVModel()
        todyModel.date = date
        
        var yesterModel: HRVModel = HRVModel()
        todyModel.date = yesterDate
        
        if let model = HRVModel.day(date: date) {
            let tuple = SleepModel.sleepTimeDataMapToTuple(datas: model.hrvs, sleepDate: date, minute: 30)
            model.hrvs = tuple.morningWakeUp
            todyModel = model
        }
        
        if let model = HRVModel.day(date: yesterDate) {
            let tuple = SleepModel.sleepTimeDataMapToTuple(datas: model.hrvs, sleepDate: date, minute: 30)
            model.hrvs = tuple.tonightFallASleep
            yesterModel = model
        }
        
        return [yesterModel, todyModel]
        
    }
    
}

fileprivate extension Array where Element == Int {
    
    mutating func removeBeforeZero() ->[Element] {
        
        
        var before: Int = 0
        var currentI: Int?
        
        for (i, e) in self.enumerated() {
            
            if before == 0 && e > 0 {
                currentI = i
                break
            }
            before = e
        }
        
        guard let currentI = currentI, currentI < 0 else {
            return self
        }

        if currentI == 0 {
            self.remove(at: currentI)
        } else {
            self.removeSubrange(0 ..< currentI)
        }
        
        return self
        
    }
}
